//
//  ReweetedStatusView.swift
//  SinaWeibo
//
//  Created by liyue on 15/11/17.
//  Copyright © 2015年 李越. All rights reserved.
//

import UIKit
import SnapKit

class ReweetedStatusView: UIView {

    var bottomConstraints: Constraint?
    
    var retweetedStatus: Status? {
        
        didSet{
            
            retweetedLabel.text = retweetedStatus?.text
            self.bottomConstraints?.uninstall()
            if let picurls = retweetedStatus?.imageURLs where picurls.count > 0 {
                pictureView.imageURLs = picurls
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
            make.height.equalTo(100)
            make.width.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    private lazy var retweetedLabel: UILabel = UILabel(title: "转发微博", size: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    private lazy var pictureView: StatusPictureView = StatusPictureView()

}
